package com.ymatou.iapi.idcardmng.testcase;

import java.io.File;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.idcardmng.parameter.SubmitIdCardInfoAndSqPicReq;
import com.ymatou.iapi.idcardmng.service.SubmitIdCardInfoAndSqPicCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.sqlwapper.YmtIdCardInfoWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
 * 上传身份证信息和图片接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SubmitIdCardInfoAndSqPic {
	private static SubmitIdCardInfoAndSqPicReq submitIdCardInfoAndSqPicReq;
	private static SubmitIdCardInfoAndSqPicCall submitIdCardInfoAndSqPicCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("神器上传身份证信息和图片接口");
	}

	@Before
	public void caseUp() {
		submitIdCardInfoAndSqPicReq = new SubmitIdCardInfoAndSqPicReq();
		submitIdCardInfoAndSqPicCall = new SubmitIdCardInfoAndSqPicCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(submitIdCardInfoAndSqPicCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDb() throws Exception {

		long buyerId = submitIdCardInfoAndSqPicReq.getBuyerId();
		YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();
		String oldKey = "1234567890123456";
		String newKey = "123456789";

		YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

		Logger.comment("校验身份证信息表ymt_idcardinfo");

		Logger.verifyEquals((int) buyerId, ymtIdCardInfo.getBuyerId(),
				"校验buyerid");

		Logger.verifyEquals(submitIdCardInfoAndSqPicReq.getName(),
				ymtIdCardInfo.getName(), "校验name");

		Logger.verifyEquals(submitIdCardInfoAndSqPicReq.getCardType(),
				ymtIdCardInfo.getCardType(), "校验CardType");

		Logger.verifyEquals(0, ymtIdCardInfo.getStatus(), "校验Status");
		Logger.verifyEquals(submitIdCardInfoAndSqPicReq.isHasPic(),
				ymtIdCardInfo.getHasPic(), "校验HasPic");
		Logger.verifyEquals(submitIdCardInfoAndSqPicReq.getOperater(),
				ymtIdCardInfo.getOperater(), "校验Operater");

		Logger.verifyEquals(0, ymtIdCardInfo.getRejectReason(),
				"校验RejectReason");
		Logger.verifyEquals(0, ymtIdCardInfo.getIsDelete(), "校验IsDelete");

		Logger.verifyEquals(
				CipherUtil.encryptAES(oldKey,
						submitIdCardInfoAndSqPicReq.getCardNumber()),
				ymtIdCardInfo.getIDCard(), "校验IDCard");
		Logger.verifyEquals(
				ymtIdCardInfo.getPhone() != null ? CipherUtil.encryptAES(
						oldKey, submitIdCardInfoAndSqPicReq.getMobile()) : null,
				ymtIdCardInfo.getPhone(), "校验phone");

		if (ymtIdCardInfo.getPhone() != null) {
			
			String plainText = CipherUtil.decryptAES(oldKey,
					ymtIdCardInfo.getPhone());
			Logger.verifyEquals(CipherUtil.encryptAES(newKey, plainText),
					ymtIdCardInfo.getMobile(), "校验Mobile");
			Logger.verifyEquals(plainText, ymtIdCardInfo.getTempPhone(),
					"校验TempPhone");
			
		} else
			Logger.verifyEquals(CipherUtil.encryptAES(newKey, submitIdCardInfoAndSqPicReq.getMobile()),
					ymtIdCardInfo.getMobile(), "校验Mobile");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_001() {
		Logger.start(true, "上传身份证信息和图片成功,发送mq消息通知交易,贝海");
		try {

			String fliename = "http://pic1.ymatou.com/G01/shangou/M00/3B/07/rBBlD1oxAT-AOo8SAAkVVGMybN8515_4_3_n_w_l.jpg";
			String fliename2 = "http://pic1.ymatou.com/G01/shangou/M00/39/E2/rBBlD1l1nbmAQp_0AAl5WLU-YRY110_4_3_n_w_l.jpg";

			submitIdCardInfoAndSqPicReq.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			submitIdCardInfoAndSqPicReq.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitIdCardInfoAndSqPicReq.setCardType(1);
			submitIdCardInfoAndSqPicReq.setHasPic(true);
			submitIdCardInfoAndSqPicReq.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitIdCardInfoAndSqPicReq.setName(TestDataManager.getChineseName());
			submitIdCardInfoAndSqPicReq.setOperater("autotest");

			submitIdCardInfoAndSqPicReq.setReverseSide(fliename);
			submitIdCardInfoAndSqPicReq.setRightSide(fliename2);

			submitIdCardInfoAndSqPicCall.setData(submitIdCardInfoAndSqPicReq);
			submitIdCardInfoAndSqPicCall.callService();
			Logger.verifyEquals("200", submitIdCardInfoAndSqPicCall.getString("Code"),
					"验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_002() {
		Logger.start(true, "用户id,与姓名在本地库存在,提交更新身份证号码成功,发送mq消息通知交易,贝海");
		try {

			String fliename = "http://pic1.ymatou.com/G01/shangou/M00/3B/07/rBBlD1oxAT-AOo8SAAkVVGMybN8515_4_3_n_w_l.jpg";
			String fliename2 = "http://pic1.ymatou.com/G01/shangou/M00/39/E2/rBBlD1l1nbmAQp_0AAl5WLU-YRY110_4_3_n_w_l.jpg";

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();

			YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = ymtIdCardInfo.getName();

			submitIdCardInfoAndSqPicReq.setBuyerId(buyerId);
			submitIdCardInfoAndSqPicReq.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitIdCardInfoAndSqPicReq.setCardType(1);
			submitIdCardInfoAndSqPicReq.setHasPic(true);
			submitIdCardInfoAndSqPicReq.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitIdCardInfoAndSqPicReq.setName(name);
			submitIdCardInfoAndSqPicReq.setOperater("autotest");
			submitIdCardInfoAndSqPicReq.setReverseSide(fliename);
			submitIdCardInfoAndSqPicReq.setRightSide(fliename2);

			submitIdCardInfoAndSqPicCall.setData(submitIdCardInfoAndSqPicReq);
			submitIdCardInfoAndSqPicCall.callService();

			Logger.verifyEquals("200", submitIdCardInfoAndSqPicCall.getString("Code"),
					"验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_003() {
		Logger.start(true, "买家Id为null");
		try {

			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCard.jpg";

			String fliename2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCardOpposite.jpg";


			submitIdCardInfoAndSqPicReq.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitIdCardInfoAndSqPicReq.setCardType(1);
			submitIdCardInfoAndSqPicReq.setHasPic(true);
			submitIdCardInfoAndSqPicReq.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitIdCardInfoAndSqPicReq.setName(TestDataManager.getChineseName());
			submitIdCardInfoAndSqPicReq.setOperater("autotest");

			submitIdCardInfoAndSqPicReq.setReverseSide(fliename);
			submitIdCardInfoAndSqPicReq.setRightSide(fliename2);

			submitIdCardInfoAndSqPicCall.setData(submitIdCardInfoAndSqPicReq);
			submitIdCardInfoAndSqPicCall.callService();

			Logger.verifyEquals("400", submitIdCardInfoAndSqPicCall.getString("Code"),
					"验证Code");

			Logger.verifyEquals("100", submitIdCardInfoAndSqPicCall.getString("BCode"),
					"验证BCode");

			Logger.verifyEquals("buyerId:买家Id必须大于0",
					submitIdCardInfoAndSqPicCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_004() {
		Logger.start(true, "身份证正面 base64 格式rightSide字符串为null");
		try {
			
			String fliename2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCardOpposite.jpg";

			submitIdCardInfoAndSqPicReq.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			submitIdCardInfoAndSqPicReq.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitIdCardInfoAndSqPicReq.setCardType(1);
			submitIdCardInfoAndSqPicReq.setHasPic(true);
			submitIdCardInfoAndSqPicReq.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitIdCardInfoAndSqPicReq.setName(TestDataManager.getChineseName());
			submitIdCardInfoAndSqPicReq.setOperater("autotest");

			submitIdCardInfoAndSqPicReq.setReverseSide(fliename2);

			submitIdCardInfoAndSqPicCall.setData(submitIdCardInfoAndSqPicReq);
			submitIdCardInfoAndSqPicCall.callService();

			Logger.verifyEquals("500", submitIdCardInfoAndSqPicCall.getString("Code"),
					"验证Code");

			Logger.verifyEquals("保存神器身份证图片失败！",
					submitIdCardInfoAndSqPicCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_005() {
		Logger.start(true, "身份证反面 base64格式reverseSide字符串为null");
		try {

			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCard.jpg";
			
			submitIdCardInfoAndSqPicReq.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			submitIdCardInfoAndSqPicReq.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitIdCardInfoAndSqPicReq.setCardType(1);
			submitIdCardInfoAndSqPicReq.setHasPic(true);
			submitIdCardInfoAndSqPicReq.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitIdCardInfoAndSqPicReq.setName(TestDataManager.getChineseName());
			submitIdCardInfoAndSqPicReq.setOperater("autotest");

			submitIdCardInfoAndSqPicReq.setRightSide(fliename);

			submitIdCardInfoAndSqPicCall.setData(submitIdCardInfoAndSqPicReq);
			submitIdCardInfoAndSqPicCall.callService();

			Logger.verifyEquals("500", submitIdCardInfoAndSqPicCall.getString("Code"),
					"验证Code");

			Logger.verifyEquals("保存神器身份证图片失败！",
					submitIdCardInfoAndSqPicCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_007() {
		Logger.start(true, "证件号为null");
		try {

			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCard.jpg";

			String fliename2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCardOpposite.jpg";
			
			submitIdCardInfoAndSqPicReq.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			
			submitIdCardInfoAndSqPicReq.setCardType(1);
			submitIdCardInfoAndSqPicReq.setHasPic(true);
			submitIdCardInfoAndSqPicReq.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitIdCardInfoAndSqPicReq.setName(TestDataManager.getChineseName());
			submitIdCardInfoAndSqPicReq.setOperater("autotest");

			submitIdCardInfoAndSqPicReq.setReverseSide(fliename);
			submitIdCardInfoAndSqPicReq.setRightSide(fliename2);

			submitIdCardInfoAndSqPicCall.setData(submitIdCardInfoAndSqPicReq);
			submitIdCardInfoAndSqPicCall.callService();
			Logger.verifyEquals("400", submitIdCardInfoAndSqPicCall.getString("Code"),
					"验证Code");
			Logger.verifyEquals("100", submitIdCardInfoAndSqPicCall.getString("BCode"),
					"验证BCode");
			Logger.verifyEquals("cardNumber:证件号不能为空",
					submitIdCardInfoAndSqPicCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_009() {
		Logger.start(true, "姓名name为null");
		try {

			String fliename = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCard.jpg";

			String fliename2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "idCardOpposite.jpg";

			submitIdCardInfoAndSqPicReq.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			submitIdCardInfoAndSqPicReq.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitIdCardInfoAndSqPicReq.setCardType(1);
			submitIdCardInfoAndSqPicReq.setHasPic(true);
			submitIdCardInfoAndSqPicReq.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitIdCardInfoAndSqPicReq.setOperater("autotest");

			submitIdCardInfoAndSqPicReq.setReverseSide(fliename);
			submitIdCardInfoAndSqPicReq.setRightSide(fliename2);

			submitIdCardInfoAndSqPicCall.setData(submitIdCardInfoAndSqPicReq);
			submitIdCardInfoAndSqPicCall.callService();
			Logger.verifyEquals("400", submitIdCardInfoAndSqPicCall.getString("Code"),
					"验证Code");
			Logger.verifyEquals("100", submitIdCardInfoAndSqPicCall.getString("BCode"),
					"验证BCode");
			Logger.verifyEquals("name:名称不能为空",
					submitIdCardInfoAndSqPicCall.getString("Msg"), "验证Msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P3.class })
	@TestCase
	public void Tc_SubmitIdCardInfoAndPic_010() {
		Logger.start(true, "cardType为2");
		try {

			String fliename = "http://pic1.ymatou.com/G01/shangou/M00/3B/07/rBBlD1oxAT-AOo8SAAkVVGMybN8515_4_3_n_w_l.jpg";
			String fliename2 = "http://pic1.ymatou.com/G01/shangou/M00/39/E2/rBBlD1l1nbmAQp_0AAl5WLU-YRY110_4_3_n_w_l.jpg";

			submitIdCardInfoAndSqPicReq.setBuyerId(Long.valueOf(EnvSetup
					.getData("SellerId2")));
			submitIdCardInfoAndSqPicReq.setCardNumber(TestDataManager
					.getCardId(20, 10));
			submitIdCardInfoAndSqPicReq.setCardType(2);
			submitIdCardInfoAndSqPicReq.setHasPic(true);
			submitIdCardInfoAndSqPicReq.setMobile("136"
					+ TestDataManager.getRandomNum(8));
			submitIdCardInfoAndSqPicReq.setName(TestDataManager.getChineseName());
			submitIdCardInfoAndSqPicReq.setOperater("autotest");

			submitIdCardInfoAndSqPicReq.setReverseSide(fliename);
			submitIdCardInfoAndSqPicReq.setRightSide(fliename2);

			submitIdCardInfoAndSqPicCall.setData(submitIdCardInfoAndSqPicReq);
			submitIdCardInfoAndSqPicCall.callService();
			Logger.verifyEquals("200", submitIdCardInfoAndSqPicCall.getString("Code"),
					"验证Code");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) throws Exception {
		String plainText = CipherUtil.decryptAES("123456789",
				"9FBFD298A27CB3518768C27EB2472B45");
		
		System.out.println(plainText);
	}
}